package com.chenshu.mycnblog.service;

import com.chenshu.mycnblog.mapper.ArticleMapper;
import com.chenshu.mycnblog.model.ArticleInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA
 * Description：文章表服务层
 * User：chenshu
 * Date：2024-01-02
 * Time：11:33
 */
@Service
public class ArticleService {
    @Autowired
    private ArticleMapper articleMapper;
    public List<ArticleInfo> getMyArticleList(Integer uid) {
        return articleMapper.selectByUid(uid);
    }

    public ArticleInfo getDetail(Integer id) {
        return articleMapper.selectById(id);
    }

    //新增文章业务返回自增id
    public Integer addArticle(ArticleInfo articleInfo) {
        articleMapper.insertArticle(articleInfo);
        return articleInfo.getId();
    }

    public Integer editArticle(ArticleInfo articleInfo) {
        return articleMapper.updateArticle(articleInfo);
    }

    /**
     *
     * @param offset
     * @param pageSize
     * @return 分页查询文章列表
     */
    public List<ArticleInfo> getArticleListByLimit(Integer offset, Integer pageSize) {
        return articleMapper.selectByLimit(offset, pageSize);
    }

    public Integer getTotalPage(Integer pageSize) {
        Integer articleCount = articleMapper.selectArticleCount();
        double totalPage = (double) articleCount /pageSize;
        //进1
        return (int) Math.ceil(totalPage);
    }

    public Integer deleteArticle(Integer id) {
        return articleMapper.deleteArticleById(id);
    }
}
